
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>摘要 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-codeblock-label/block.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-click-reveal/click_reveal.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="part1-lex.html" />
    
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    实验简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../log.html">
            
                <a href="../log.html">
            
                    
                    更新日志
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">零：前置准备</li>
        
        
    
        <li class="chapter " data-level="2.1" >
            
                <span>
            
                    
                    配环境、跑测试
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../lab0/env.html">
            
                <a href="../lab0/env.html">
            
                    
                    环境配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../lab0/testing.html">
            
                <a href="../lab0/testing.html">
            
                    
                    运行测试样例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../lab0/riscv.html">
            
                <a href="../lab0/riscv.html">
            
                    
                    RISC-V 的工具链使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">一：第一个编译器</li>
        
        
    
        <li class="chapter " data-level="3.1" >
            
                <span>
            
                    
                    仅一个 return 的 main 函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="3.1.1" data-path="part0-intro.html">
            
                <a href="part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="part1-lex.html">
            
                <a href="part1-lex.html">
            
                    
                    词法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="part1-1-task.html">
            
                <a href="part1-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" data-path="part2-parse.html">
            
                <a href="part2-parse.html">
            
                    
                    语法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="part2-1-rdp.html">
            
                <a href="part2-1-rdp.html">
            
                    
                    递归下降分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.6" data-path="part2-2-task.html">
            
                <a href="part2-2-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.7" data-path="part3-ir.html">
            
                <a href="part3-ir.html">
            
                    
                    中间码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.8" data-path="part3-1-task.html">
            
                <a href="part3-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.9" data-path="part4-codegen.html">
            
                <a href="part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.10" data-path="part4-1-task.html">
            
                <a href="part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.11" data-path="summary.html">
            
                <a href="summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.2" >
            
                <span>
            
                    
                    [dzy] 仅一个 return 的 main 函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.1" data-path="../lab1alt/part1.html">
            
                <a href="../lab1alt/part1.html">
            
                    
                    第一部分：从零开始的 lexer、parser 以及汇编生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.2" data-path="../lab1alt/part2.html">
            
                <a href="../lab1alt/part2.html">
            
                    
                    第二部分：词法语法分析工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.3" data-path="../lab1alt/part3.html">
            
                <a href="../lab1alt/part3.html">
            
                    
                    第三部分：使用中间码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.4" >
            
                <span>
            
                    
                    其他
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.4.1" data-path="../lab1alt/antlr.html">
            
                <a href="../lab1alt/antlr.html">
            
                    
                    ANTLR 使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.4.2" data-path="../lab1alt/visitor.html">
            
                <a href="../lab1alt/visitor.html">
            
                    
                    Visitor 模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">二：常量表达式</li>
        
        
    
        <li class="chapter " data-level="4.1" >
            
                <span>
            
                    
                    步骤二
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../lab2/part0-intro.html">
            
                <a href="../lab2/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../lab2/summary.html">
            
                <a href="../lab2/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.2" >
            
                <span>
            
                    
                    步骤三
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.2.1" data-path="../lab3/part0-intro.html">
            
                <a href="../lab3/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2.2" data-path="../lab3/summary.html">
            
                <a href="../lab3/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.3" >
            
                <span>
            
                    
                    步骤四
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.3.1" data-path="../lab4/part0-intro.html">
            
                <a href="../lab4/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.2" data-path="../lab4/summary.html">
            
                <a href="../lab4/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">三：变量和语句</li>
        
        
    
        <li class="chapter " data-level="5.1" >
            
                <span>
            
                    
                    步骤五
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.1" data-path="../lab5/part0-intro.html">
            
                <a href="../lab5/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.2" data-path="../lab5/stackframe.html">
            
                <a href="../lab5/stackframe.html">
            
                    
                    栈帧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.3" data-path="../lab5/summary.html">
            
                <a href="../lab5/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5.2" >
            
                <span>
            
                    
                    步骤六
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.2.1" data-path="../lab6/part0-intro.html">
            
                <a href="../lab6/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2.2" data-path="../lab6/summary.html">
            
                <a href="../lab6/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">四：块语句和作用域和更多语句</li>
        
        
    
        <li class="chapter " data-level="6.1" >
            
                <span>
            
                    
                    步骤七
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1.1" data-path="../lab7/part0-intro.html">
            
                <a href="../lab7/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.2" data-path="../lab7/namer.html">
            
                <a href="../lab7/namer.html">
            
                    
                    名称解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.3" data-path="../lab7/summary.html">
            
                <a href="../lab7/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6.2" >
            
                <span>
            
                    
                    步骤八
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.2.1" data-path="../lab8/part0-intro.html">
            
                <a href="../lab8/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.2" data-path="../lab8/part4-codegen.html">
            
                <a href="../lab8/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.3" data-path="../lab8/part4-1-task.html">
            
                <a href="../lab8/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.4" data-path="../lab8/summary.html">
            
                <a href="../lab8/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">五：函数和全局变量</li>
        
        
    
        <li class="chapter " data-level="7.1" >
            
                <span>
            
                    
                    步骤九
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.1" data-path="../lab9/part0-intro.html">
            
                <a href="../lab9/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.2" data-path="../lab9/part4-1-cconv.html">
            
                <a href="../lab9/part4-1-cconv.html">
            
                    
                    调用约定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.3" data-path="../lab9/part4-2-codegen.html">
            
                <a href="../lab9/part4-2-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.4" data-path="../lab9/part4-3-task.html">
            
                <a href="../lab9/part4-3-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.5" data-path="../lab9/summary.html">
            
                <a href="../lab9/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.2" >
            
                <span>
            
                    
                    步骤十
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.2.1" data-path="../lab10/part0-intro.html">
            
                <a href="../lab10/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.2" data-path="../lab10/part4-codegen.html">
            
                <a href="../lab10/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.3" data-path="../lab10/part4-1-task.html">
            
                <a href="../lab10/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.4" data-path="../lab10/summary.html">
            
                <a href="../lab10/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">六：指针和数组</li>
        
        
    
        <li class="chapter " data-level="8.1" >
            
                <span>
            
                    
                    步骤十一
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1.1" data-path="../lab11/part0-intro.html">
            
                <a href="../lab11/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.2" data-path="../lab11/typeck.html">
            
                <a href="../lab11/typeck.html">
            
                    
                    类型检查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.3" data-path="../lab11/summary.html">
            
                <a href="../lab11/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8.2" >
            
                <span>
            
                    
                    步骤十二
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.2.1" data-path="../lab12/part0-intro.html">
            
                <a href="../lab12/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2.2" data-path="../lab12/summary.html">
            
                <a href="../lab12/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">参考资料</li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../../REFERENCE.html">
            
                <a href="../../REFERENCE.html">
            
                    
                    参考资料
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >摘要</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="lab1&#xFF1A;&#x6574;&#x6570;">Lab1&#xFF1A;&#x6574;&#x6570;</h1>
<p>&#x8FD9;&#x662F;&#x5173;&#x4E8E;&#x7F16;&#x5199;minidecaf&#x7F16;&#x8BD1;&#x5668;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x6B65;&#x9AA4;&#x3002;</p>
<p>&#x7F16;&#x5199;minidecaf&#x7F16;&#x8BD1;&#x5668;&#x662F;&#x53C2;&#x8003;&#x4E86;&#x4F7F;&#x7528;Abdulaziz Ghuloum&#x7684;<a href="http://scheme2006.cs.uchicago.edu/11-ghuloum.pdf" target="_blank">An Incremental Approach to Compiler Construction</a>&#x548C;Nora Sandler&#x7684;<a href="https://norasandler.com/2017/11/29/Write-a-Compiler.html" target="_blank">Writing a C Compiler</a>&#x4F5C;&#x4E3A;&#x8DEF;&#x7EBF;&#x56FE;&#x3002;&#x8FD9;&#x91CC;&#x7684;minidecaf&#x8BED;&#x8A00;&#x57FA;&#x672C;&#x4E0A;&#x662F;C&#x8BED;&#x8A00;&#x7684;&#x4E00;&#x4E2A;&#x5B50;&#x96C6;&#x3002;&#x4F60;&#x901A;&#x8FC7;&#x4ECE;&#x7F16;&#x8BD1;minidecaf&#x6E90;&#x8BED;&#x8A00;&#x7684;&#x4E00;&#x4E2A;&#x5FAE;&#x4E0D;&#x8DB3;&#x9053;&#x7684;&#x5B50;&#x96C6;&#x5F00;&#x59CB;&#xFF0C;&#x80FD;&#x591F;&#x751F;&#x6210;<a href="https://github.com/decaf-lang/minidecaf/blob/master/doc/riscv-assembly-intro.md" target="_blank">RISC-V&#x6C47;&#x7F16;&#x4EE3;&#x7801;</a>&#xFF0C;&#x5E76;&#x80FD;&#x5728;RISC-V&#x673A;&#x5668;&#xFF08;&#x76EE;&#x524D;&#x662F;&#x57FA;&#x4E8E;QEMU&#x6A21;&#x62DF;&#x5668;&#xFF09;&#x4E0A;&#x8FD0;&#x884C;/&#x6D4B;&#x8BD5;&#x4F60;&#x5199;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#x751F;&#x6210;&#x7684;&#x6700;&#x7EC8;&#x673A;&#x5668;&#x4EE3;&#x7801;&#x3002;&#x7136;&#x540E;&#x4F60;&#x518D;&#x4E00;&#x6B65;&#x4E00;&#x6B65;&#x5730;&#x6DFB;&#x52A0;&#x65B0;&#x7684;&#x8BED;&#x8A00;&#x7279;&#x6027;&#x3002;&#x5728;&#x7B2C;&#x4E00;&#x6B65;&#x4E2D;&#xFF0C;&#x4F60;&#x53EA;&#x662F;&#x8FD4;&#x56DE;&#x5E38;&#x91CF;&#xFF1B;&#x5728;&#x540E;&#x9762;&#x7684;&#x6B65;&#x9AA4;&#x4E2D;&#xFF0C;&#x4F60;&#x5904;&#x7406;&#x52A0;&#x6CD5;&#x548C;&#x51CF;&#x6CD5;&#xFF1B;&#x4EE5;&#x6B64;&#x7C7B;&#x63A8;&#x3002;&#x6BCF;&#x4E00;&#x6B65;&#x90FD;&#x5C0F;&#x5230;&#x8DB3;&#x4EE5;&#x8BA9;&#x4EBA;&#x611F;&#x89C9;&#x5230;&#x6613;&#x4E8E;&#x7BA1;&#x7406;&#xFF0C;&#x800C;&#x5728;&#x6BCF;&#x4E00;&#x6B65;&#x7ED3;&#x675F;&#x65F6;&#xFF0C;&#x4F60;&#x90FD;&#x6709;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x5DE5;&#x4F5C;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#x3002;&#x53E6;&#x5916;&#xFF0C;&#x901A;&#x8FC7;&#x8DB3;&#x591F;&#x8BE6;&#x5C3D;&#x7684;<a href="https://github.com/decaf-lang/minidecaf-tests" target="_blank">&#x6D4B;&#x8BD5;&#x7A0B;&#x5E8F;</a>&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x968F;&#x65F6;&#x9A8C;&#x8BC1;&#x4F60;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#x5728;&#x6BCF;&#x6B21;&#x66F4;&#x65B0;&#x540E;&#x662F;&#x5426;&#x6B63;&#x5E38;&#x5DE5;&#x4F5C;&#x3002;</p>
<h1 id="&#x524D;&#x8A00;">&#x524D;&#x8A00;</h1>
<p>&#x5728;&#x4F60;&#x5F00;&#x59CB;&#x4E4B;&#x524D;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x51B3;&#x5B9A;&#x4E24;&#x4EF6;&#x4E8B;&#xFF1A;&#x7528;&#x4EC0;&#x4E48;&#x8BED;&#x8A00;&#x6765;&#x5199;&#x4F60;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#xFF0C;&#x4EE5;&#x53CA;&#x5982;&#x4F55;&#x5904;&#x7406;&#x8BCD;&#x6CD5;&#x5206;&#x6790;&#xFF08;lexing&#xFF09;&#x548C;&#x8BED;&#x6CD5;&#x89E3;&#x6790;&#xFF08;parsing &#xFF09; &#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x7528;&#x4EFB;&#x4F55;&#x4F60;&#x559C;&#x6B22;&#x7684;&#x8BED;&#x8A00;&#x6765;&#x5B9E;&#x73B0;&#x7F16;&#x8BD1;&#x5668;</p>
<blockquote>
<p>&#x63D0;&#x793A;&#xFF1A;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#x5177;&#x6709;&#x548C;<a href="https://chadaustin.me/2015/07/sum-types/" target="_blank">sum type</a>&#x548C;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#xFF08; pattern matching&#xFF09;&#x7684;&#x8BED;&#x8A00;&#xFF0C;&#x6BD4;&#x5982;OCaml&#x3001;Haskell&#x6216;Rust&#x3002;&#x5982;&#x679C;&#x4F60;&#x8FD9;&#x6837;&#x505A;&#x7684;&#x8BDD;&#xFF0C;&#x6784;&#x5EFA;&#x548C;&#x904D;&#x5386;&#x4E00;&#x4E2A;AST&#x4F1A;&#x53D8;&#x5F97;&#x66F4;&#x52A0;&#x7B80;&#x5355;&#x3002;&#x524D;&#x63D0;&#x662F;&#x4F60;&#x80FD;&#x63A5;&#x53D7;&#x5B66;&#x4E60;&#x548C;&#x638C;&#x63E1;&#x8FD9;&#x4E9B;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x7684;&#x6240;&#x6295;&#x5165;&#x7684;&#x65F6;&#x95F4;&#x4E0E;&#x7CBE;&#x529B;&#x3002;</p>
</blockquote>
<p>&#x4F60;&#x8FD8;&#x9700;&#x8981;&#x51B3;&#x5B9A;&#x662F;&#x81EA;&#x5DF1;&#x5199;&#x8BED;&#x6CD5;&#x89E3;&#x6790;&#x5668;&#x548C;&#x8BCD;&#x6CD5;&#x5206;&#x6790;&#x5668;&#xFF0C;&#x8FD8;&#x662F;&#x4F7F;&#x7528;&#x81EA;&#x52A8;&#x89E3;&#x6790;&#x5668;&#x548C;&#x626B;&#x63CF;&#x5668;&#x751F;&#x6210;&#x5668;&#xFF08;&#x4F8B;&#x5982;<a href="https://github.com/westes/flex" target="_blank">flex</a>&#xFF0C;<a href="https://www.gnu.org/software/bison/" target="_blank">bison</a>&#xFF0C;<a href="https://www.antlr.org" target="_blank">antlr4</a>&#xFF09;&#x3002;&#x5728;&#x6574;&#x4E2A;&#x5B9E;&#x9A8C;&#x73AF;&#x8282;&#x4E2D;&#xFF0C;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x90FD;&#x4F1A;&#x63D0;&#x4F9B;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x5C55;&#x793A;&#x5982;&#x4F55;&#x624B;&#x5DE5;&#x7F16;&#x5199;&#x4E00;&#x4E2A;&#x8BCD;&#x6CD5;&#x5668;&#xFF08;&#x6216;&#x626B;&#x63CF;&#x5668;&#xFF09;&#x548C;&#x9012;&#x5F52;&#x4E0B;&#x964D;&#x89E3;&#x6790;&#x5668;&#x3002;&#x4F7F;&#x7528;&#x89E3;&#x6790;&#x5668;&#x751F;&#x6210;&#x5668;&#x53EF;&#x80FD;&#x66F4;&#x5BB9;&#x6613;&#xFF0C;&#x7F3A;&#x70B9;&#x662F;&#x4E86;&#x89E3;&#x5E95;&#x5C42;&#x8FD0;&#x884C;&#x7EC6;&#x8282;&#x548C;&#x8C03;&#x8BD5;bug&#x53EF;&#x80FD;&#x4F1A;&#x56F0;&#x96BE;&#x4E00;&#x4E9B;&#x3002;&#x5982;&#x679C;&#x80FD;&#x591F;&#x76F4;&#x63A5;&#x8BBE;&#x8BA1;&#x5B9E;&#x73B0;&#x89E3;&#x6790;&#x5668;&#x751F;&#x6210;&#x5668;&#xFF0C;&#x90A3;&#x4E48;&#x5BF9;&#x7F16;&#x8BD1;&#x8BFE;&#x4E0A;&#x8BB2;&#x7684;&#x5F88;&#x591A;&#x539F;&#x7406;&#x3001;&#x7B97;&#x6CD5;&#x7684;&#x7406;&#x89E3;&#x4F1A;&#x66F4;&#x52A0;&#x6DF1;&#x5165;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x8BBE;&#x8BA1;&#x5B9E;&#x73B0;&#x89E3;&#x6790;&#x5668;&#x751F;&#x6210;&#x5668;&#x4E0D;&#x662F;&#x57FA;&#x672C;&#x5B9E;&#x9A8C;&#x8981;&#x6C42;&#x3002;</p>
</blockquote>
<p>&#x672C;&#x9636;&#x6BB5;&#x6211;&#x4EEC;&#x5C06;&#x7F16;&#x8BD1;&#x4E00;&#x4E2A;&#x8FD4;&#x56DE;&#x5355;&#x4E2A;&#x6574;&#x6570;&#x7684;minidecaf&#x7A0B;&#x5E8F;&#x3002;&#x6211;&#x4EEC;&#x8FD8;&#x5C06;&#x5EFA;&#x7ACB;&#x7F16;&#x8BD1;&#x5668;&#x7684;&#x4E09;&#x4E2A;&#x57FA;&#x672C;&#x9636;&#x6BB5;&#xFF08;pass&#xFF09;&#xFF1A;&#x8BCD;&#x6CD5;&#x3001;&#x89E3;&#x6790;&#x548C;&#x4EE3;&#x7801;&#x751F;&#x6210;&#x3002;&#x3002;&#x7B2C;&#x4E00;&#x6B65;&#x5C06;&#x6709;&#x6BD4;&#x8F83;&#x5927;&#x7684;&#x5DE5;&#x4F5C;&#x91CF;&#xFF0C;&#x5373;&#x5EFA;&#x7ACB;&#x4E86;&#x4E00;&#x4E2A;&#x7F16;&#x8BD1;&#x5668;&#x7684;&#x6846;&#x67B6;&#xFF0C;&#x8BE5;&#x6846;&#x67B6;&#x5C06;&#x4F7F;&#x4EE5;&#x540E;&#x6DFB;&#x52A0;&#x66F4;&#x591A;&#x8BED;&#x8A00;&#x7279;&#x6027;&#x53D8;&#x5F97;&#x5BB9;&#x6613;&#xFF0C;&#x5BF9;&#x540E;&#x7EED;&#x5B9E;&#x9A8C;&#x6B65;&#x9AA4;&#x6709;&#x8F83;&#x5927;&#x7684;&#x5E2E;&#x52A9;&#xFF0C;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x6211;&#x4EEC;&#x8981;&#x7F16;&#x8BD1;&#x7684;&#x7A0B;&#x5E8F; return_2.c</p>
<pre><code>int main() {
    return 2;
}
</code></pre><p>&#x6211;&#x4EEC;&#x5C06;&#x53EA;&#x5904;&#x7406;&#x6709;&#x4E00;&#x4E2A;&#x51FD;&#x6570; &quot;main &quot;&#x7684;&#x7A0B;&#x5E8F;&#xFF0C;&#x5B83;&#x7531;&#x4E00;&#x4E2A;&#x8FD4;&#x56DE;&#x8BED;&#x53E5;&#x7EC4;&#x6210;&#x3002;&#x552F;&#x4E00;&#x4E0D;&#x540C;&#x7684;&#x662F;&#x8FD4;&#x56DE;&#x7684;&#x6574;&#x6570;&#x7684;&#x503C;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x4F1A;&#x5904;&#x7406;&#x5341;&#x516D;&#x8FDB;&#x5236;&#x6216;&#x516B;&#x8FDB;&#x5236;&#x7684;&#x6574;&#x6570;&#xFF0C;&#x53EA;&#x5904;&#x7406;&#x5341;&#x8FDB;&#x5236;&#x3002;&#x6211;&#x4EEC;&#x4E0D;&#x4F1A;&#x5904;&#x7406;&#x5341;&#x516D;&#x8FDB;&#x5236;&#x6216;&#x516B;&#x8FDB;&#x5236;&#x7684;&#x6574;&#x6570;&#xFF0C;&#x53EA;&#x5904;&#x7406;&#x5341;&#x8FDB;&#x5236;&#x3002;&#x4E3A;&#x4E86;&#x9A8C;&#x8BC1;&#x4F60;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#x662F;&#x5426;&#x6B63;&#x5E38;&#x5DE5;&#x4F5C;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x7F16;&#x8BD1;&#x4E00;&#x4E2A;&#x7A0B;&#x5E8F;&#xFF0C;&#x8FD0;&#x884C;&#x5B83;&#xFF0C;&#x5E76;&#x68C0;&#x67E5;&#x5B83;&#x7684;&#x8FD4;&#x56DE;&#x4EE3;&#x7801;&#x3002;</p>
<pre><code>$ YOUR_COMPILER return_2.c # &#x7528;&#x4F8B;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x628A;return_2.c&#x7F16;&#x8BD1;&#x4E3A; return.s &#x6C47;&#x7F16;&#x7A0B;&#x5E8F;
$ riscv64-unknown-elf-gcc return_2.s -o return_2 # riscv-64&#x6C47;&#x7F16;&#x5668;&#x628A;return.s&#x7FFB;&#x8BD1;&#x4E3A;return_2&#x6267;&#x884C;&#x7A0B;&#x5E8F;
$ qemu-riscv64 ./return_2 # &#x7528;QEMU for riscv-64&#x786C;&#x4EF6;&#x6A21;&#x62DF;&#x5668;&#x8FD0;&#x884C;return_2&#x6267;&#x884C;&#x7A0B;&#x5E8F;
$ echo $? # &#x68C0;&#x67E5;return_2&#x6267;&#x884C;&#x7A0B;&#x5E8F;&#x7684;&#x6267;&#x884C;&#x7ED3;&#x679C;&#xFF0C;&#x5E94;&#x8BE5;&#x662F; 2
2
</code></pre><p>&#x4F60;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x4EA7;&#x751F;risc-v&#x6C47;&#x7F16;&#x4EE3;&#x7801;&#x3002;&#x6211;&#x4EEC;&#x4E0D;&#x4F1A;&#x81EA;&#x5DF1;&#x5C06;&#x6C47;&#x7F16;&#x6587;&#x4EF6;&#x8F6C;&#x5316;&#x4E3A;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;--&#x90A3;&#x662F;&#x6C47;&#x7F16;&#x5668;&#x548C;&#x94FE;&#x63A5;&#x5668;&#x7684;&#x5DE5;&#x4F5C;&#xFF08;&#x6700;&#x597D;&#x6709;&#x4E2A;&#x94FE;&#x63A5;&#x4ECB;&#x7ECD;&#x6C47;&#x7F16;&#x5668;&#x548C;&#x8FDE;&#x63A5;&#x5668;&#xFF09;&#x3002;&#x4E3A;&#x4E86;&#x770B;&#x770B;&#x8FD9;&#x4E2A;&#x7A0B;&#x5E8F;&#x5728;&#x6C47;&#x7F16;&#x4E2D;&#x7684;&#x6837;&#x5B50;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x7528;GCC&#x6765;&#x7F16;&#x8BD1;&#x5B83;&#x3002;</p>
<pre><code>$ riscv64-unknown-elf-gcc -S -O3 return_2.c
$ cat return_2.s
    .file    &quot;return_2.c&quot;
......
    .globl    main
    .type    main, @function
main:
    li    a0,2
    ret
......
</code></pre><p>&#x73B0;&#x5728;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x770B;&#x770B;&#x6C47;&#x7F16;&#x7A0B;&#x5E8F;&#x672C;&#x8EAB;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5FFD;&#x7565;<code>.section</code>&#x3001;<code>.align</code>&#x7B49;&#x6307;&#x4EE4;&#xFF0C;&#x8FD9;&#x4E9B;&#x6C47;&#x7F16;&#x539F;&#x8BED;&#x53EF;&#x53C2;&#x52A0;<a href="https://github.com/decaf-lang/minidecaf/blob/master/doc/riscv-assembly-directives.md" target="_blank">&#x8FD9;&#x91CC;&#x7684;&#x4ECB;&#x7ECD;</a>&#x3002;--&#x5982;&#x679C;&#x4F60;&#x5220;&#x9664;&#x5B83;&#x4EEC;&#xFF0C;&#x4F60;&#x4ECD;&#x7136;&#x53EF;&#x4EE5;&#x751F;&#x6210;&#x5E76;&#x8FD0;&#x884C;return_2&#x6267;&#x884C;&#x7A0B;&#x5E8F;&#x3002;<code>.globl main</code>&#x8868;&#x793A;<code>main</code>&#x7B26;&#x53F7;&#x5E94;&#x8BE5;&#x5BF9;&#x94FE;&#x63A5;&#x5668;&#x53EF;&#x89C1;&#xFF0C;&#x5426;&#x5219;&#x5B83;&#x627E;&#x4E0D;&#x5230;&#x7A0B;&#x5E8F;&#x7684;&#x5165;&#x53E3;&#x70B9;&#x3002;</p>
<p>&#x6700;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x6709;&#x4E86;&#x5B9E;&#x9645;&#x7684;&#x6C47;&#x7F16;&#x6307;&#x4EE4;&#x3002;</p>
<pre><code>main:                  ; label for start of &quot;main&quot; function
    movl    $2, %eax    ; move constant &quot;2&quot; into the a0 register
    ret                 ; return from function
</code></pre><p>&#x8FD9;&#x91CC;&#x6700;&#x91CD;&#x8981;&#x7684;&#x4E00;&#x70B9;&#x662F;&#xFF0C;&#x5F53;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x65F6;&#xFF0C;<code>a0</code>&#x5BC4;&#x5B58;&#x5668;&#x5C06;&#x5305;&#x542B;&#x5176;&#x8FD4;&#x56DE;&#x503C;&#x3002;<code>main</code>&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x5C06;&#x662F;&#x7A0B;&#x5E8F;&#x7684;&#x9000;&#x51FA;&#x4EE3;&#x7801;&#x3002;&#x5728;&#x4E0A;&#x9762;&#x7684;&#x6C47;&#x7F16;&#x7247;&#x6BB5;&#x4E2D;&#xFF0C;&#x552F;&#x4E00;&#x53EF;&#x4EE5;&#x6539;&#x53D8;&#x7684;&#x662F;&#x8FD4;&#x56DE;&#x503C;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x6BCF;&#x5F53;&#x4F60;&#x5728;&#x9605;&#x8BFB;&#x6C47;&#x7F16;&#x65F6;&#xFF0C;&#x8BF7;&#x786E;&#x4FDD;&#x4F60;&#x77E5;&#x9053;&#x5B83;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x4EC0;&#x4E48;&#x8BED;&#x6CD5;!</p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                
                <a href="part1-lex.html" class="navigation navigation-next navigation-unique" aria-label="Next page: 词法分析">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"摘要","level":"3.1.1","depth":2,"next":{"title":"词法分析","level":"3.1.2","depth":2,"path":"docs/lab1/part1-lex.md","ref":"docs/lab1/part1-lex.md","articles":[]},"previous":{"title":"仅一个 return 的 main 函数","level":"3.1","depth":1,"ref":"","articles":[{"title":"摘要","level":"3.1.1","depth":2,"path":"docs/lab1/part0-intro.md","ref":"docs/lab1/part0-intro.md","articles":[]},{"title":"词法分析","level":"3.1.2","depth":2,"path":"docs/lab1/part1-lex.md","ref":"docs/lab1/part1-lex.md","articles":[]},{"title":"任务","level":"3.1.3","depth":2,"path":"docs/lab1/part1-1-task.md","ref":"docs/lab1/part1-1-task.md","articles":[]},{"title":"语法分析","level":"3.1.4","depth":2,"path":"docs/lab1/part2-parse.md","ref":"docs/lab1/part2-parse.md","articles":[]},{"title":"递归下降分析","level":"3.1.5","depth":2,"path":"docs/lab1/part2-1-rdp.md","ref":"docs/lab1/part2-1-rdp.md","articles":[]},{"title":"任务","level":"3.1.6","depth":2,"path":"docs/lab1/part2-2-task.md","ref":"docs/lab1/part2-2-task.md","articles":[]},{"title":"中间码","level":"3.1.7","depth":2,"path":"docs/lab1/part3-ir.md","ref":"docs/lab1/part3-ir.md","articles":[]},{"title":"任务","level":"3.1.8","depth":2,"path":"docs/lab1/part3-1-task.md","ref":"docs/lab1/part3-1-task.md","articles":[]},{"title":"代码生成","level":"3.1.9","depth":2,"path":"docs/lab1/part4-codegen.md","ref":"docs/lab1/part4-codegen.md","articles":[]},{"title":"任务","level":"3.1.10","depth":2,"path":"docs/lab1/part4-1-task.md","ref":"docs/lab1/part4-1-task.md","articles":[]},{"title":"小结","level":"3.1.11","depth":2,"path":"docs/lab1/summary.md","ref":"docs/lab1/summary.md","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-element","chapter-fold","katex","alerts","emphasize","mermaid-gb3","codeblock-label","code","search-pro","click-reveal","expandable-chapters-interactive","localized-footer"],"pluginsConfig":{"chapter-fold":{},"emphasize":{},"codeblock-label":{},"search-pro":{},"search":{},"localized-footer":{"filename":"gitalk.html","hline":"true"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"hide-element":{},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":1},"click-reveal":{},"highlight":{},"mermaid-gb3":{},"expandable-chapters-interactive":{},"alerts":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"docs/lab1/part0-intro.md","mtime":"2020-08-20T12:03:36.499Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-08-26T15:46:29.226Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-click-reveal/click_reveal.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

